<template>
  <div class="block">
    <div v-if="!postersManage">
      <Error></Error>
    </div>
    <div v-if="postersManage">
      <!--编辑弹框-->
      <el-dialog title="添加海报" :visible.sync="dialogFormVisible">
        <el-form :model="formDate" label-width="100px" ref="formDate" >
          <el-form-item label="上传图片">
            <label>
              <span class="uploadingImage">
                <i class="el-icon-plus" id="icon"></i>
                <input type="file" style="display: none" @change="changeHeard" id="files_img"></input>
              </span>
            </label>
          </el-form-item>
          <el-form-item label="排序" label-width="100px" style="width: 390px;">
            <el-input v-model="formDate.sort" type="text" maxlength="2" placeholder="请输入1~99的正整数"
                      oninput="if(this.value=='0'){this.value='';}else{this.value=this.value.replace(/[^0-9]/g,'')}"
            ></el-input>
          </el-form-item>
          <el-form-item label="是否发布" label-width="100px" style="margin-bottom: 0">
            <el-radio-group v-model="formDate.exhibition">
              <el-radio label="1">不发布</el-radio>
              <el-radio label="2">发布</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="是否热门" label-width="100px" style="margin-bottom: 0">
            <el-radio-group v-model="formDate.bestSeller">
              <el-radio label="1">不热卖</el-radio>
              <el-radio label="2">热卖</el-radio>
            </el-radio-group>
          </el-form-item>
          <!--<el-form-item label="是否推荐" label-width="100px" style="margin-bottom: 0">-->
            <!--<el-radio-group v-model="formDate.recommend">-->
              <!--<el-radio label="1">不推荐</el-radio>-->
              <!--<el-radio label="2">推荐</el-radio>-->
            <!--</el-radio-group>-->
          <!--</el-form-item>-->
          <el-form-item label="所属项目" label-width="100px">
            <el-radio-group v-model="formDate.posters">
              <el-radio v-for="(item,key,index) in posters_arr" :label="key" :key="index">{{item}}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="submitFormDate()">确 定</el-button>
        </div>
      </el-dialog>
      <div class="inquireDiv">
        <p>
          <label><span>发布状态：</span>
            <el-select v-model="form.issueStatus" placeholder="请选择发布状态">
              <el-option label="全部" value="0"></el-option>
              <el-option label="发布" value="2"></el-option>
              <el-option label="不发布" value="1"></el-option>
            </el-select>
          </label>
          <label><span>热门状态：</span>
            <el-select v-model="form.hotStatus" placeholder="请选择热门状态">
              <el-option label="全部" value="0"></el-option>
              <el-option label="热门" value="2"></el-option>
              <el-option label="不热门" value="1"></el-option>
            </el-select>
          </label>
          <label><span>所属项目：</span>
            <el-select v-model="form.project" placeholder="请选择状态">
              <el-option label="全部" value="0"></el-option>
              <el-option v-for="(item,key,index) in posters_arr" :label="item" :value="key" :key="index"></el-option>
            </el-select>
          </label>
          <el-button type="primary" @click="examineFn" style="float: right">查 询</el-button>
        </p>
      </div>
      <div class="tableDiv" v-loading="loading">
        <el-row>
          <el-table
            :cell-class-name="addClass"
            :data="tableData"
            border
            stripe>
            <el-table-column
              type="index"
              align="center"
              width="50"
              label="序号">
            </el-table-column>
            <el-table-column
              label="海报列表"
              width="80">
              <template slot-scope="scope">
                <el-popover placement="top-start" title="" trigger="click">
                  <img :src="scope.row.img_url" alt="" style="width: 250px;cursor: pointer">
                  <img slot="reference" :src="scope.row.img_url" style="width: 60px;cursor: pointer">
                </el-popover>
                <!--　　　　<img :src="scope.row.img_url" width="80"/>-->
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              label="发布状态"
              width="80">
              <template slot-scope="scope">
                <span style="color:#F56C6C" v-if="scope.row.pi_status === 1">不发布</span>
                <span style="color:#67C23A" v-if="scope.row.pi_status === 2">发布</span>
              </template>
            </el-table-column>
            <el-table-column
              prop="name"
              align="center"
              label="热门状态"
              width="80">
              <template slot-scope="scope">
                <span style="color:#F56C6C" v-if="scope.row.pi_is_hot === 1">不热门</span>
                <span style="color:#67C23A" v-if="scope.row.pi_is_hot === 2">热门</span>
              </template>
            </el-table-column>
            <!--<el-table-column-->
              <!--align="center"-->
              <!--label="推荐状态"-->
              <!--width="80">-->
              <!--<template slot-scope="scope">-->
                <!--<span style="color:#F56C6C" v-if="scope.row.pi_is_recommend === 1">不推荐</span>-->
                <!--<span style="color:#67C23A" v-if="scope.row.pi_is_recommend === 2">推荐</span>-->
              <!--</template>-->
            <!--</el-table-column>-->
            <el-table-column
              prop="project"
              label="所属项目"
              align="center"
              width="100"
            >
            </el-table-column>
            <el-table-column
              prop="time"
              sortable
              label="创建时间"
              align="center"
              width="160"
            >
            </el-table-column>
            <el-table-column
              align="center">
              <template slot="header" slot-scope="scope">
                <div class="el-table-header">
                  <el-button type="primary" @click.native.prevent="addInformation">添加海报</el-button>
                </div>
              </template>
              <template slot-scope="scope">
                <el-button v-show="scope.row.pi_status !== 1"
                           @click.native.prevent="showRow(scope.$index,tableData,1,1,1)"
                           type="warning"
                           size="small">
                  不发布
                </el-button>
                <el-button v-show="scope.row.pi_status === 1"
                           @click.native.prevent="showRow(scope.$index,tableData,2,2,1)"
                           type="success"
                           size="small">
                  发布
                </el-button>
                <el-button v-show="scope.row.pi_is_hot !== 1"
                           @click.native.prevent="showRow(scope.$index,tableData,1,3,3)"
                           type="warning"
                           size="small">
                  不热门
                </el-button>
                <el-button v-show="scope.row.pi_is_hot === 1"
                           @click.native.prevent="showRow(scope.$index,tableData,2,4,3)"
                           type="success"
                           size="small">
                  热门
                </el-button>
                <!--<el-button v-show="scope.row.pi_is_recommend !== 1"-->
                           <!--@click.native.prevent="showRow(scope.$index,tableData,1,5,2)"-->
                           <!--type="warning"-->
                           <!--size="small">-->
                  <!--不推荐-->
                <!--</el-button>-->
                <!--<el-button v-show="scope.row.pi_is_recommend === 1"-->
                           <!--@click.native.prevent="showRow(scope.$index,tableData,2,6,2)"-->
                           <!--type="success"-->
                           <!--size="small">-->
                  <!--推荐-->
                <!--</el-button>-->
                <!--<el-button-->
                  <!--@click.native.prevent="deleteRow(scope.$index,tableData)"-->
                  <!--type="danger"-->
                  <!--size="small">-->
                  <!--删除-->
                <!--</el-button>-->
              </template>
            </el-table-column>
          </el-table>
          <el-col :span="24">
            <div class="block" v-if="total">
              <el-pagination
                @current-change="handleCurrentChange"
                :current-page="currentPage"
                :page-size="10"
                layout="total, prev, pager, next"
                :total="total">
              </el-pagination>
            </div>
          </el-col>
        </el-row>
      </div>
    </div>
  </div>
</template>

<script>
  import Error from '../errorPage/errorPage401'
  export default {
    data() {
      return {
        mid: '',
        loading: true,
        currentPage: '',
        tableData: [],
        total: '',
        postersManage:false,
        dialogFormVisible:false,
        formDate:{
          imageUrl:'',
          exhibition:'1',
          bestSeller:'1',
          recommend:'1',
          posters:'',
          sort:''
        },
        form:{
          issueStatus:'',
          hotStatus:'',
          project:'',
        },
        posters_arr:[]
      }
    },
    methods: {
      //初始化
      initFn: function (page) {
        let data = {
          page: page,
          status:this.form.issueStatus,
          hot:this.form.hotStatus,
          project:this.form.project,
        };
        let str = this.encryptionAesRsa(data);
        let encryptData = str.split('%$#@zjt@#$%')[0];
        let encryptKey = str.split('%$#@zjt@#$%')[1];
        let incomData = {
          map: 'api_posters_list',
          plum_session_api: localStorage.getItem('token'),
          encryptData: encryptKey,
          encryptKey: encryptData
        };
        this.$http.post('', incomData).then((data) => {
          if (data.ec == 200) {
            this.loading = false;
            this.total = Number(data.data.count);
            this.currentPage = Number(data.data.page);
            this.tableData = data.data.list;
            this.posters_arr = data.data.posters_arr;
          } else if (data.ec == 401) {
            this.$message.error(data.em);
            setTimeout(() => {
              this.exitLogin();
            }, 1500)
          } else {
            this.$message.error(data.em);
          }
        }).catch((err) => {
          this.$message.error('error!');
        })
      },
      //查询
      examineFn: function () {
        this.initFn(1)
      },
      //列变色
      addClass({row, column, rowIndex, columnIndex}) {
        if (columnIndex === 5) {
          return 'cell-blue'
        }
      },
      //点击分页
      handleCurrentChange(val) {
        this.$router.replace({
          path: this.$route.path,
          query: {
            page: val,
          }
        });
        this.loading = true;
        this.initFn(val);
      },
      // 展示/不展示按钮
      showRow(index, row, status,strs,type) {
        let str = '';
        switch(strs){
          case 1:
            str = '此操作将该条海报改为不发布, 是否继续?';
            break;
          case 2:
            str = '此操作会将该条海报改为发布, 是否继续?';
            break;
          case 3:
            str = '此操作会将该条海报改为不热门, 是否继续?';
            break;
          case 4:
            str = '此操作会将该条海报改为热门, 是否继续?';
            break;
          case 5:
            str = '此操作会将该条海报改为不推荐, 是否继续?';
            break;
          case 6:
            str = '此操作会将该条海报改为推荐, 是否继续?';
            break;
        }
        this.$confirm(str, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.mid = row[index].pi_id;
          let data = {
            type:type,
            id: this.mid,
            status: status
          };
          let str = this.encryptionAesRsa(data);
          let encryptData = str.split('%$#@zjt@#$%')[0];
          let encryptKey = str.split('%$#@zjt@#$%')[1];
          let incomData = {
            map: 'api_posters_update',
            plum_session_api: localStorage.getItem('token'),
            encryptData: encryptKey,
            encryptKey: encryptData
          };
          this.$http.post('', incomData).then((data) => {
            if (data.ec == 200) {
              this.$message.success(data.em);
              this.initFn(this.currentPage)
            } else if (data.ec == 401) {
              this.$message.error(data.em);
              setTimeout(() => {
                this.exitLogin();
              }, 1500)
            } else {
              this.$message.error(data.em);
            }
          }).catch((err) => {
            this.$message.error('error!');
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消操作'
          });
        });
      },
      //添加海报
      addInformation:function (index,row) {
        this.dialogFormVisible = true;
      },
      //上传图片
      changeHeard:function () {
        let that = this;
        let files_ = document.getElementById('files_img');
        let upload_file = files_.files[0];
        let fileSize = upload_file.size;
        let size = fileSize / 2048;
        let resder = new FileReader();
        resder.readAsDataURL(upload_file);
        resder.onload = function (e) {
          if(size > 1024){
            that.$message.error('上传图片最大不得超过2M');
          }else {
            that.imgUpFn(this.result);
          }
        }
      },
      //图片上传
      imgUpFn:function(imgUrl){
        let data = {
          image: imgUrl,
        };
        let str = this.encryptionAesRsa(data);
        let encryptData = str.split('%$#@zjt@#$%')[0];
        let encryptKey = str.split('%$#@zjt@#$%')[1];
        let incomData = {
          map: 'api_posters_upload',
          plum_session_api: localStorage.getItem('token'),
          encryptData: encryptKey,
          encryptKey: encryptData
        };
        this.$http.post('', incomData).then((data) => {
          if (data.ec == 200) {
            this.formDate.imageUrl = data.path;
            let uploadingImage =document.getElementsByClassName('uploadingImage')[0];
            let icon=document.getElementById("icon");
            if(icon){
              icon.style.display = 'none';
            }
            uploadingImage.style.backgroundImage = 'url('+ imgUrl+')'
            this.$message.success(data.em);
          } else if (data.ec == 401) {
            this.$message.error(data.em);
            setTimeout(() => {
              this.exitLogin();
            }, 1500)
          } else {
            this.$message.error(data.em);
          }
        }).catch((err) => {
          this.$message.error('error!');
        })
      },
      //确定提交
      submitFormDate(){
        let data = {
          img_url:this.formDate.imageUrl,
          sort:this.formDate.sort,
          img_status:this.formDate.exhibition,
          img_project:this.formDate.posters,
          is_recommend:this.formDate.recommend,
          is_hot:this.formDate.bestSeller,
        };
        let str = this.encryptionAesRsa(data);
        let encryptData = str.split('%$#@zjt@#$%')[0];
        let encryptKey = str.split('%$#@zjt@#$%')[1];
        let incomData = {
          map: 'api_posters_submit',
          plum_session_api: localStorage.getItem('token'),
          encryptData: encryptKey,
          encryptKey: encryptData
        };
        this.$http.post('', incomData).then((data) => {
          if (data.ec == 200) {
            this.$message.success(data.em);
            this.initFn(this.currentPage);
            this.dialogFormVisible = false;
            this.formDate.imageUrl = '';
            let uploadingImage =document.getElementsByClassName('uploadingImage')[0];
            uploadingImage.style.backgroundImage = 'url()';
            let icon=document.getElementById("icon");
            if(icon){
              icon.style.display = 'block';
            }
            this.formDate.exhibition = '1';
            this.formDate.bestSeller = '1';
            this.formDate.recommend = '1';
            this.formDate.posters = '';
            this.formDate.sort = '';
          } else if (data.ec == 401) {
            this.$message.error(data.em);
            setTimeout(() => {
              this.exitLogin();
            }, 1500)
          } else {
            this.$message.error(data.em);
          }
        }).catch((err) => {
          this.$message.error('error!');
        })
      },
    },
    mounted() {
      this.currentPage = this.$route.query.page ? this.$route.query.page : 1;
      this.initFn(this.currentPage);
      // 按钮权限
      let buttonMenu = localStorage.getItem('buttonMenu');
      buttonMenu = this.edecryptButton(buttonMenu);
      //页面权限
      if (buttonMenu.indexOf('postersManage') !== -1) {
        this.postersManage = true
      }
    },
    components:{
      Error
    }
  }
</script>

<style scoped>
  .inquireDiv {
    padding: 30px;
    background: #ffffff;
    box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04)
  }

  .inquireDiv label {
    display: inline-block;
    margin-left: 50px;
  }
  .inquireDiv label span{
    width: 80px;
    display: inline-block;
  }

  .inquireDiv .el-input {
    width: 220px;
  }

  .tableDiv {
    margin-top: 20px;
  }

  .el-col {
    border-radius: 4px;
    height: 75px;
    background-color: #fff;
  }

  .tableDiv >>> .el-pagination {
    height: 30px;
    line-height: 30px;
    margin-top: 25px;
    text-align: center;
  }

  .tableDiv >>> .cell-blue {
    color: #409EFF !important;
  }

  .drawerDiv {
    width: 100%;
    height: calc(90vh);
    padding: 20px;
    overflow-y: auto;
    box-sizing: border-box;
  }

  .drawerDiv h2 {
    text-align: center;
    font-size: 20px;
    margin: 20px 0;
  }

  .drawerDiv p {
    font-size: 16px;
    color: #000000;
    margin-top: 10px;
  }

  .drawerDiv p label {
    font-size: 16px;
    vertical-align: middle;
    font-weight: 600;
    display: inline-block;
    width: 80px;
    text-align: right;
  }

  .drawerDiv p span {
    font-size: 14px;
    vertical-align: middle;
    color: #555555;
    margin-left: 5px;
  }

  .el-table-header >>> .el-button {
    float: right;
    height: 40px;
  }
  .block >>> .el-dialog {
    width: 550px;
  }


  .block >>> .uploadingImage{
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    width: 200px;
    height: 355px;
    display: inline-block;
    vertical-align: middle;
    transition: all .2s;
    background-repeat: no-repeat;
    background-size: 100% 100%;
    background-position: center center;

  }
  .block >>> .uploadingImage:hover{
    border: 1px dashed #409EFF;
  }
  .block >>> .uploadingImage i{
    font-size: 30px !important;
    color: #555555;
    position: absolute;
    left: 50%;
    top: 50%;
    margin-left: -16px;
    margin-top: -15px;
  }
</style>


